package vn.gq.cd.adutils;

import java.util.Random;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.mediation.inmobi.InMobiAdapterExtras;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import vn.gq.cd.model.bean.AdmobItem;

public class BannerHelper {
	
	public static final int DELAY_TIME_FAILED;
	public static final int DELAY_TIME_RELOAD;
	
	private AdmobItem admobItem;
	private Random mRandom = new Random();
	private AdView adView;
	private BannerType adType;
	private boolean isLoadIMAd = true;
	private BannerAdListener adListener = new BannerAdListener();
	
	static {
		DELAY_TIME_FAILED = 30000;
		DELAY_TIME_RELOAD = 60000;
	}
	
	public enum BannerType {
		BANNER, O_BANNER
	}
	
	public BannerHelper() {
		
	}
	
	public BannerHelper(Activity activity, LinearLayout adLayout, BannerType adType) {
		admobItem = AdmobItem.load(activity);
		String adUnitId = (adType == BannerType.BANNER) ? admobItem.getAdTypeBanner() : admobItem.getAdTypeOBanner();
		adView = new AdView(activity, AdSize.SMART_BANNER, adUnitId);
		adLayout.addView(adView);
		adView.setAdListener(adListener);
		this.adType = adType;
	}
	
	public boolean isLoadedAd() {
		return admobItem.isLoaded();
	}
	
	public void loadAd() {
		if (adView != null) {
			adView.post(loadAdRunnable);
		}
	}
	
	public void setLoadIMAd(boolean isLoadIMAd) {
		this.isLoadIMAd = isLoadIMAd;
	}

	private Runnable loadAdRunnable = new Runnable() {
		
		@Override
		public void run() {
			AdRequest adRequest = new AdRequest();
			if (isLoadIMAd || (adType == BannerType.O_BANNER)) {
				InMobiAdapterExtras inMobiExtra = new InMobiAdapterExtras();
				inMobiExtra.setIncome(65000);
				adRequest.setNetworkExtras(inMobiExtra);
			}
			adView.loadAd(adRequest);
		}
	};
	
	public class BannerAdListener implements AdListener {

		@Override
		public void onDismissScreen(Ad ad) {
			adView.stopLoading();
			adView.removeCallbacks(loadAdRunnable);
			adView.postDelayed(loadAdRunnable, DELAY_TIME_FAILED);
		}

		@Override
		public void onFailedToReceiveAd(Ad ad, ErrorCode e) {
			
		}

		@Override
		public void onLeaveApplication(Ad ad) {
			
		}

		@Override
		public void onPresentScreen(Ad ad) {
			
		}

		@Override
		public void onReceiveAd(Ad ad) {
			ScaleAnimation zoomIn = new ScaleAnimation(0.5f, 1f, 0.5f, 1f, 
					Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
			zoomIn.setDuration(500);
			float distance = adView.getHeight();
			TranslateAnimation slideDown = new TranslateAnimation(0, 0, -distance, 0);
			slideDown.setDuration(500);
			int idx = mRandom.nextInt(2);
			if (idx == 0)
				adView.startAnimation(zoomIn);
			else
				adView.startAnimation(slideDown);
			adView.stopLoading();
			adView.removeCallbacks(loadAdRunnable);
			adView.postDelayed(loadAdRunnable, DELAY_TIME_RELOAD);
		}
		
	}
	
	public void onStop() {
		if (adView != null) {
			adView.stopLoading();
		}
	}
	
	public void onDestroy() {
		if (adView != null) {
			adView.destroy();
		}
	}
}
